{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LAB 4a:  Creating a Sampled Dataset.\n",
    "\n",
    "## Learning Objectives\n",
    "\n",
    "1. Setup up the environment.\n",
    "1. Sample the natality dataset to create train/eval/test sets.\n",
    "1. Preprocess the data in Pandas dataframe.\n",
    "\n",
    "\n",
    "## Introduction \n",
    "In this notebook, we'll read data from BigQuery into our notebook to preprocess the data within a Pandas dataframe for a small, repeatable sample.\n",
    "\n",
    "We will set up the environment, sample the natality dataset to create train/eval/test splits, and preprocess the data in a Pandas dataframe.\n",
    "\n",
    "Each learning objective will correspond to a __#TODO__ in this student lab notebook -- try to complete this notebook first and then review the [solution notebook](../solutions/4a_sample_babyweight.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "hJ7ByvoXzpVI"
   },
   "source": [
    "## Set up environment variables and load necessary libraries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "mC9K9Dpx1ztf"
   },
   "source": [
    "Check that the Google BigQuery library is installed and if not, install it. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 609
    },
    "colab_type": "code",
    "id": "RZUQtASG10xO",
    "outputId": "5612d6b0-9730-476a-a28f-8fdc14f4ecde"
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "pip freeze | grep google-cloud-bigquery==1.6.1 || \\\n",
    "pip install google-cloud-bigquery==1.6.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import necessary libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from google.cloud import bigquery\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #1: Set environment variables.\n",
    "\n",
    "Set environment variables so that we can use them throughout the entire lab. We will be using our project name for our bucket, so you only need to change your project and region."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "export PROJECT=$(gcloud config list project --format \"value(core.project)\")\n",
    "echo \"Your current GCP Project Name is: \"$PROJECT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO: Change environment variables\n",
    "PROJECT = \"cloud-training-demos\"  # Replace with your PROJECT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create ML datasets by sampling using BigQuery\n",
    "\n",
    "We'll begin by sampling the BigQuery data to create smaller datasets. Let's create a BigQuery client that we'll use throughout the lab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bq = bigquery.Client(project = PROJECT)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We need to figure out the right way to divide our hash values to get our desired splits. To do that we need to define some values to hash with in the modulo. Feel free to play around with these values to get the perfect combination."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "modulo_divisor = 100\n",
    "train_percent = 80.0\n",
    "eval_percent = 10.0\n",
    "\n",
    "train_buckets = int(modulo_divisor * train_percent / 100.0)\n",
    "eval_buckets = int(modulo_divisor * eval_percent / 100.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can make a series of queries to check if our bucketing values result in the correct sizes of each of our dataset splits and then adjust accordingly. Therefore, to make our code more compact and reusable, let's define a function to return the head of a dataframe produced from our queries up to a certain number of rows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_dataframe_head_from_query(query, count=10):\n",
    "    \"\"\"Displays count rows from dataframe head from query.\n",
    "    \n",
    "    Args:\n",
    "        query: str, query to be run on BigQuery, results stored in dataframe.\n",
    "        count: int, number of results from head of dataframe to display.\n",
    "    Returns:\n",
    "        Dataframe head with count number of results.\n",
    "    \"\"\"\n",
    "    df = bq.query(\n",
    "        query + \" LIMIT {limit}\".format(\n",
    "            limit=count)).to_dataframe()\n",
    "\n",
    "    return df.head(count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For our first query, we're going to use the original query above to get our label, features, and columns to combine into our hash which we will use to perform our repeatable splitting. There are only a limited number of years, months, days, and states in the dataset. Let's see what the hash values are. We will need to include all of these extra columns to hash on to get a fairly uniform spread of the data. Feel free to try less or more in the hash and see how it changes your results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get label, features, and columns to hash and split into buckets\n",
    "hash_cols_fixed_query = \"\"\"\n",
    "SELECT\n",
    "    weight_pounds,\n",
    "    is_male,\n",
    "    mother_age,\n",
    "    plurality,\n",
    "    gestation_weeks,\n",
    "    year,\n",
    "    month,\n",
    "    CASE\n",
    "        WHEN day IS NULL THEN\n",
    "            CASE\n",
    "                WHEN wday IS NULL THEN 0\n",
    "                ELSE wday\n",
    "            END\n",
    "        ELSE day\n",
    "    END AS date,\n",
    "    IFNULL(state, \"Unknown\") AS state,\n",
    "    IFNULL(mother_birth_state, \"Unknown\") AS mother_birth_state\n",
    "FROM\n",
    "    publicdata.samples.natality\n",
    "WHERE\n",
    "    year > 2000\n",
    "    AND weight_pounds > 0\n",
    "    AND mother_age > 0\n",
    "    AND plurality > 0\n",
    "    AND gestation_weeks > 0\n",
    "\"\"\"\n",
    "\n",
    "display_dataframe_head_from_query(hash_cols_fixed_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using `COALESCE` would provide the same result as the nested `CASE WHEN`. This is preferable when all we want is the first non-null instance. To be precise the `CASE WHEN` would become `COALESCE(wday, day, 0) AS date`. You can read more about it [here](https://cloud.google.com/bigquery/docs/reference/standard-sql/conditional_expressions)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next query will combine our hash columns and will leave us just with our label, features, and our hash values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_query = \"\"\"\n",
    "SELECT\n",
    "    weight_pounds,\n",
    "    is_male,\n",
    "    mother_age,\n",
    "    plurality,\n",
    "    gestation_weeks,\n",
    "    FARM_FINGERPRINT(\n",
    "        CONCAT(\n",
    "            CAST(year AS STRING),\n",
    "            CAST(month AS STRING),\n",
    "            CAST(date AS STRING),\n",
    "            CAST(state AS STRING),\n",
    "            CAST(mother_birth_state AS STRING)\n",
    "        )\n",
    "    ) AS hash_values\n",
    "FROM\n",
    "    ({CTE_hash_cols_fixed})\n",
    "\"\"\".format(CTE_hash_cols_fixed=hash_cols_fixed_query)\n",
    "\n",
    "display_dataframe_head_from_query(data_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next query is going to find the counts of each of the unique 657484 `hash_values`. This will be our first step at making actual hash buckets for our split via the `GROUP BY`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the counts of each of the unique hashs of our splitting column\n",
    "first_bucketing_query = \"\"\"\n",
    "SELECT\n",
    "    hash_values,\n",
    "    COUNT(*) AS num_records\n",
    "FROM\n",
    "    ({CTE_data})\n",
    "GROUP BY\n",
    "    hash_values\n",
    "\"\"\".format(CTE_data=data_query)\n",
    "\n",
    "display_dataframe_head_from_query(first_bucketing_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The query below performs a second layer of bucketing where now for each of these bucket indices we count the number of records."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the number of records in each of the hash buckets\n",
    "second_bucketing_query = \"\"\"\n",
    "SELECT\n",
    "    ABS(MOD(hash_values, {modulo_divisor})) AS bucket_index,\n",
    "    SUM(num_records) AS num_records\n",
    "FROM\n",
    "    ({CTE_first_bucketing})\n",
    "GROUP BY\n",
    "    ABS(MOD(hash_values, {modulo_divisor}))\n",
    "\"\"\".format(\n",
    "    CTE_first_bucketing=first_bucketing_query, modulo_divisor=modulo_divisor)\n",
    "\n",
    "display_dataframe_head_from_query(second_bucketing_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The number of records is hard for us to easily understand the split, so we will normalize the count into percentage of the data in each of the hash buckets in the next query."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate the overall percentages\n",
    "percentages_query = \"\"\"\n",
    "SELECT\n",
    "    bucket_index,\n",
    "    num_records,\n",
    "    CAST(num_records AS FLOAT64) / (\n",
    "    SELECT\n",
    "        SUM(num_records)\n",
    "    FROM\n",
    "        ({CTE_second_bucketing})) AS percent_records\n",
    "FROM\n",
    "    ({CTE_second_bucketing})\n",
    "\"\"\".format(CTE_second_bucketing=second_bucketing_query)\n",
    "\n",
    "display_dataframe_head_from_query(percentages_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll now select the range of buckets to be used in training."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Choose hash buckets for training and pull in their statistics\n",
    "train_query = \"\"\"\n",
    "SELECT\n",
    "    *,\n",
    "    \"train\" AS dataset_name\n",
    "FROM\n",
    "    ({CTE_percentages})\n",
    "WHERE\n",
    "    bucket_index >= 0\n",
    "    AND bucket_index < {train_buckets}\n",
    "\"\"\".format(\n",
    "    CTE_percentages=percentages_query,\n",
    "    train_buckets=train_buckets)\n",
    "\n",
    "display_dataframe_head_from_query(train_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll do the same by selecting the range of buckets to be used evaluation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Choose hash buckets for validation and pull in their statistics\n",
    "eval_query = \"\"\"\n",
    "SELECT\n",
    "    *,\n",
    "    \"eval\" AS dataset_name\n",
    "FROM\n",
    "    ({CTE_percentages})\n",
    "WHERE\n",
    "    bucket_index >= {train_buckets}\n",
    "    AND bucket_index < {cum_eval_buckets}\n",
    "\"\"\".format(\n",
    "    CTE_percentages=percentages_query,\n",
    "    train_buckets=train_buckets,\n",
    "    cum_eval_buckets=train_buckets + eval_buckets)\n",
    "\n",
    "display_dataframe_head_from_query(eval_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lastly, we'll select the hash buckets to be used for the test split."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Choose hash buckets for testing and pull in their statistics\n",
    "test_query = \"\"\"\n",
    "SELECT\n",
    "    *,\n",
    "    \"test\" AS dataset_name\n",
    "FROM\n",
    "    ({CTE_percentages})\n",
    "WHERE\n",
    "    bucket_index >= {cum_eval_buckets}\n",
    "    AND bucket_index < {modulo_divisor}\n",
    "\"\"\".format(\n",
    "    CTE_percentages=percentages_query,\n",
    "    cum_eval_buckets=train_buckets + eval_buckets,\n",
    "    modulo_divisor=modulo_divisor)\n",
    "\n",
    "display_dataframe_head_from_query(test_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the below query, we'll `UNION ALL` all of the datasets together so that all three sets of hash buckets will be within one table. We added `dataset_id` so that we can sort on it in the query after."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Union the training, validation, and testing dataset statistics\n",
    "union_query = \"\"\"\n",
    "SELECT\n",
    "    0 AS dataset_id,\n",
    "    *\n",
    "FROM\n",
    "    ({CTE_train})\n",
    "UNION ALL\n",
    "SELECT\n",
    "    1 AS dataset_id,\n",
    "    *\n",
    "FROM\n",
    "    ({CTE_eval})\n",
    "UNION ALL\n",
    "SELECT\n",
    "    2 AS dataset_id,\n",
    "    *\n",
    "FROM\n",
    "    ({CTE_test})\n",
    "\"\"\".format(CTE_train=train_query, CTE_eval=eval_query, CTE_test=test_query)\n",
    "\n",
    "display_dataframe_head_from_query(union_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lastly, we'll show the final split between train, eval, and test sets. We can see both the number of records and percent of the total data. It is really close to the 80/10/10 that we were hoping to get."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Show final splitting and associated statistics\n",
    "split_query = \"\"\"\n",
    "SELECT\n",
    "    dataset_id,\n",
    "    dataset_name,\n",
    "    SUM(num_records) AS num_records,\n",
    "    SUM(percent_records) AS percent_records\n",
    "FROM\n",
    "    ({CTE_union})\n",
    "GROUP BY\n",
    "    dataset_id,\n",
    "    dataset_name\n",
    "ORDER BY\n",
    "    dataset_id\n",
    "\"\"\".format(CTE_union=union_query)\n",
    "\n",
    "display_dataframe_head_from_query(split_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #1: Sample BigQuery dataset.\n",
    "\n",
    "Sample the BigQuery result set (above) so that you have approximately 8,000 training examples and 1000 evaluation examples.\n",
    "The training and evaluation datasets have to be well-distributed (not all the babies are born in Jan 2005, for example)\n",
    "and should not overlap (no baby is part of both training and evaluation datasets).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we know that our splitting values produce a good global splitting on our data, here's a way to get a well-distributed portion of the data in such a way that the train/eval/test sets do not overlap and takes a subsample of our global splits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# every_n allows us to subsample from each of the hash values\n",
    "# This helps us get approximately the record counts we want\n",
    "every_n = # TODO: Experiment with values to get close to target counts\n",
    "\n",
    "# TODO: Replace FUNC with correct function to split with\n",
    "# TODO: Replace COLUMN with correct column to split on\n",
    "splitting_string = \"ABS(FUNC(COLUMN, {0} * {1}))\".format(every_n, modulo_divisor)\n",
    "\n",
    "def create_data_split_sample_df(query_string, splitting_string, lo, up):\n",
    "    \"\"\"Creates a dataframe with a sample of a data split.\n",
    "\n",
    "    Args:\n",
    "        query_string: str, query to run to generate splits.\n",
    "        splitting_string: str, modulo string to split by.\n",
    "        lo: float, lower bound for bucket filtering for split.\n",
    "        up: float, upper bound for bucket filtering for split.\n",
    "    Returns:\n",
    "        Dataframe containing data split sample.\n",
    "    \"\"\"\n",
    "    query = \"SELECT * FROM ({0}) WHERE {1} >= {2} and {1} < {3}\".format(\n",
    "        query_string, splitting_string, int(lo), int(up))\n",
    "\n",
    "    df = bq.query(query).to_dataframe()\n",
    "\n",
    "    return df\n",
    "\n",
    "train_df = create_data_split_sample_df(\n",
    "    data_query, splitting_string,\n",
    "    lo=0, up=train_percent)\n",
    "\n",
    "eval_df = create_data_split_sample_df(\n",
    "    data_query, splitting_string,\n",
    "    lo=train_percent, up=train_percent + eval_percent)\n",
    "\n",
    "test_df = create_data_split_sample_df(\n",
    "    data_query, splitting_string,\n",
    "    lo=train_percent + eval_percent, up=modulo_divisor)\n",
    "\n",
    "print(\"There are {} examples in the train dataset.\".format(len(train_df)))\n",
    "print(\"There are {} examples in the validation dataset.\".format(len(eval_df)))\n",
    "print(\"There are {} examples in the test dataset.\".format(len(test_df)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preprocess data using Pandas\n",
    "\n",
    "We'll perform a few preprocessing steps to the data in our dataset. Let's add extra rows to simulate the lack of ultrasound. That is we'll duplicate some rows and make the `is_male` field be `Unknown`. Also, if there is more than child we'll change the `plurality` to `Multiple(2+)`. While we're at it, we'll also change the plurality column to be a string. We'll perform these operations below. \n",
    "\n",
    "Let's start by examining the training dataset as is."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also, notice that there are some very important numeric fields that are missing in some rows (the count in Pandas doesn't count missing data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is always crucial to clean raw data before using in machine learning, so we have a preprocessing step. We'll define a `preprocess` function below. Note that the mother's age is an input to our model so users will have to provide the mother's age; otherwise, our service won't work. The features we use for our model were chosen because they are such good predictors and because they are easy enough to collect."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Task #2: Pandas preprocessing.\n",
    "\n",
    "Use Pandas to:\n",
    "* Clean up the data to remove rows that are missing any of the fields.\n",
    "* Simulate the lack of ultrasound.\n",
    "* Change the plurality column to be a string.\n",
    "\n",
    "Hint (highlight to see): <p>\n",
    "Filtering:\n",
    "<pre style=\"color:white\">\n",
    "df = df[df.weight_pounds > 0]\n",
    "</pre>\n",
    "Modify plurality to be a string:\n",
    "<pre style=\"color:white\">\n",
    "twins_etc = dict(zip([1,2,3,4,5],\n",
    "                   [\"Single(1)\", \"Twins(2)\", \"Triplets(3)\", \"Quadruplets(4)\", \"Quintuplets(5)\"]))\n",
    "df[\"plurality\"].replace(twins_etc, inplace=True)\n",
    "</pre>\n",
    "Lack of ultrasound:\n",
    "<pre style=\"color:white\">\n",
    "no_ultrasound = df.copy(deep=True)\n",
    "no_ultrasound[\"is_male\"] = \"Unknown\"\n",
    "</pre>\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def preprocess(df):\n",
    "    \"\"\" Preprocess pandas dataframe for augmented babyweight data.\n",
    "    \n",
    "    Args:\n",
    "        df: Dataframe containing raw babyweight data.\n",
    "    Returns:\n",
    "        Pandas dataframe containing preprocessed raw babyweight data as well\n",
    "            as simulated no ultrasound data masking some of the original data.\n",
    "    \"\"\"\n",
    "    # Clean up raw data\n",
    "    # TODO: Filter out what we don\"t want to use for training\n",
    "\n",
    "\n",
    "    # TODO: Modify plurality field to be a string\n",
    "\n",
    "\n",
    "    # TODO: Clone data and mask certain columns to simulate lack of ultrasound\n",
    "\n",
    "    # TODO: Modify is_male\n",
    "    \n",
    "    # TODO: Modify plurality\n",
    "\n",
    "    # Concatenate both datasets together and shuffle\n",
    "    return pd.concat(\n",
    "        [df, no_ultrasound]).sample(frac=1).reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's process the train/eval/test set and see a small sample of the training data after our preprocessing:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df = preprocess(train_df)\n",
    "eval_df = preprocess(eval_df)\n",
    "test_df = preprocess(test_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's look again at a summary of the dataset. Note that we only see numeric columns, so `plurality` does not show up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write to .csv files \n",
    "\n",
    "In the final versions, we want to read from files, not Pandas dataframes. So, we write the Pandas dataframes out as csv files. Using csv files gives us the advantage of shuffling during read. This is important for distributed training because some workers might be slower than others, and shuffling the data helps prevent the same data from being assigned to the slow workers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define columns\n",
    "columns = [\"weight_pounds\",\n",
    "           \"is_male\",\n",
    "           \"mother_age\",\n",
    "           \"plurality\",\n",
    "           \"gestation_weeks\"]\n",
    "\n",
    "# Write out CSV files\n",
    "train_df.to_csv(\n",
    "    path_or_buf=\"train.csv\", columns=columns, header=False, index=False)\n",
    "eval_df.to_csv(\n",
    "    path_or_buf=\"eval.csv\", columns=columns, header=False, index=False)\n",
    "test_df.to_csv(\n",
    "    path_or_buf=\"test.csv\", columns=columns, header=False, index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "wc -l *.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "head *.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "tail *.csv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lab Summary: \n",
    "In this lab, we set up the environment, sampled the natality dataset to create train/eval/test splits, and preprocessed the data in a Pandas dataframe."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright 2022 Google Inc. Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
